import javax.swing.*;
import java.awt.*;

public class GridBagLayoutExample {
    public static void main(String[] args) {
        JFrame frame = new JFrame("GridBagLayout 示例");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setSize(400, 300);

        JPanel panel = new JPanel(new GridBagLayout());
        frame.add(panel);

        GridBagConstraints gbc = new GridBagConstraints();
        gbc.fill = GridBagConstraints.NONE; // 不让组件填充区域

        // 设置按钮之间的间距为0
        gbc.insets = new Insets(0, 0, 0, 0);

        // 添加多个按钮到网格中
        for (int row = 0; row < 2; row++) {
            for (int col = 0; col < 3; col++) {
                JButton button = new JButton("Button " + (row * 3 + col + 1));
                gbc.gridx = col; // 设置列位置
                gbc.gridy = row; // 设置行位置
                panel.add(button, gbc);
            }
        }

        frame.setVisible(true);
    }
}
